package com.qm.fingertest;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.serialport.SerialPortFinder;
import android.text.TextPaint;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import com.qm.finger.FingerManager;
import com.qm.model.ReadData;
import com.qm.utils.FingerListen;
import com.qm.utils.ReadCollectUtils;
import com.qm.utils.ReadCompareUtils;
import com.qm.utils.ReadRegisterUtils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {
    public static String TAG = "finger-test";
    private InputStream mInputStream;
    private int userID = 1;//注册用户 id
    private int count = 0;//总共采集三次
    private int compareTimes = 3;//比对次数 设置最大为3次
    private int compareCount = 1;
    private ImageView iv_img;
    private String[] mDevices;
    private Spinner mSpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_img = findViewById(R.id.iv_img);
        mSpinner = findViewById(R.id.spinner);
        SerialPortFinder serialPortFinder = new SerialPortFinder();
        // 设备
        mDevices = serialPortFinder.getAllDevicesPath();
        if (mDevices.length == 0) {
            mDevices = new String[]{
                    "找不到设备"
            };
        }
        ArrayAdapter<String> deviceAdapter =
                new ArrayAdapter<String>(this, R.layout.spinner_item_vict, mDevices);
        mSpinner.setAdapter(deviceAdapter);
        mSpinner.setSelection(0);
    }

    /**
     * 打开串口
     */
    public void open(View view) {
        String serial = mSpinner.getSelectedItem().toString();
        mInputStream = FingerManager.getInstance().Init(serial);
        if (mInputStream != null) {
            Toast.makeText(this, "串口打开成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "串口打开失败", Toast.LENGTH_SHORT).show();
        }
    }


    /**
     * 开始采集第一次指纹
     */
    public void start(View view) {
        if (mInputStream != null) {
            int userNum = FingerManager.getInstance().getUserNum();
            if (userNum >= 0) {
                userID = userNum + 1;
            }
            count = 0;
            int ret = FingerManager.getInstance().startFinger_1(userID);
            if (ret == 0) {
                checkRegisterData();
                count++;
            } else {
                Toast.makeText(MainActivity.this, "采集指纹失败，请重新采集", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(MainActivity.this, "请先打开串口", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 获取用户数量
     */
    public void getUserNum(View view) {
        if ((mInputStream != null)) {
            int ret = FingerManager.getInstance().getUserNum();
            Toast.makeText(this, "当前用户数量：" + ret, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "请先打开串口", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 删除所有用户
     */
    public void deleteAllUser(View view) {
        if ((mInputStream != null)) {
            int ret = FingerManager.getInstance().deleteAllUserByRole(0);
            if (ret == 0) {
                Toast.makeText(this, "删除用户成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(MainActivity.this, "请先打开串口", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 比对特征值
     */
    public void compare(View view) {
        if (mInputStream != null) {
            compareCount = 1;
            int ret = FingerManager.getInstance().compareUser();
            if (ret != 0) {//开始采集指纹信息
                ReadRegisterUtils.getInstance().closeFinger();
                Toast.makeText(MainActivity.this, "指纹器初始化失败，请重新初始化", Toast.LENGTH_SHORT).show();
            } else {
                checkCompareData();
            }
        } else {
            Toast.makeText(MainActivity.this, "请先打开串口", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 采集图像并提取特征值
     */
    public void collect(View view) {
        if (mInputStream != null) {
            int ret = FingerManager.getInstance().collectImgExtractF();
            if (ret != 0) {//开始采集指纹信息
                ReadRegisterUtils.getInstance().closeFinger();
                Toast.makeText(MainActivity.this, "指纹器初始化失败，请重新初始化", Toast.LENGTH_SHORT).show();
            } else {
                checkCollectData();
            }
        } else {
            Toast.makeText(MainActivity.this, "请先打开串口", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 刪除指定用戶
     */
    public void deleteUser(View view) {
        if (mInputStream!=null){
            int ret = FingerManager.getInstance().deleteUser(userID);
            if (ret==0){
                Toast.makeText(MainActivity.this, userID+"用户已经删除", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this, "用户删除失败", Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(MainActivity.this, "请先打开串口", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 监听采集指纹信息  并提取特征
     */
    private void checkCollectData() {
        ReadCollectUtils.getInstance().startCollectRead(new FingerListen() {
            @Override
            public void onFail() {
                ReadCollectUtils.getInstance().closeFinger();
            }

            @Override
            public void onSuccess(Object readData) {
                byte[] fingerData = (byte[]) readData;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (fingerData != null) {
                            int ret = FingerManager.getInstance().getUserNum();
                            if (ret >= 0) {
                                userID = ret + 1;
                                int ret_1 = FingerManager.getInstance().registerUser(userID, fingerData);
                                if (ret_1 != 0) {//开始采集指纹信息
                                    ReadRegisterUtils.getInstance().closeFinger();
                                    Toast.makeText(MainActivity.this, "指纹信息登记失败", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(MainActivity.this, "指纹写入成功", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                Toast.makeText(MainActivity.this, "获取用户数量失败", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(MainActivity.this, "指纹信息登记失败-数据为空", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                ReadCollectUtils.getInstance().closeFinger();
            }
        }, mInputStream);
    }

    /**
     * 监听1:N比对  比对3次  不成功直接停止
     */
    private void checkCompareData() {
        ReadCompareUtils.getInstance().startCompareRead(new FingerListen() {
            @Override
            public void onFail() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, "指纹器初始化失败", Toast.LENGTH_SHORT).show();
                        ReadCompareUtils.getInstance().closeFinger();
                    }
                });
            }

            @Override
            public void onSuccess(Object readData) {
                int ret = ((ReadData) readData).getRet();
                int user_ID = ((ReadData) readData).getUserID();
                switch (ret) {
                    case 5://不存在该用户信息
                        if (compareCount > compareTimes) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this, "不存在该用户，请先注册！", Toast.LENGTH_SHORT).show();
                                    ReadCompareUtils.getInstance().closeFinger();
                                }
                            });
                        } else {
                            int ret_ = FingerManager.getInstance().compareUser();
                            if (ret_ != 0) {//开始采集指纹信息
                                ReadRegisterUtils.getInstance().closeFinger();
                                Toast.makeText(MainActivity.this, "指纹器初始化失败，请重新初始化", Toast.LENGTH_SHORT).show();
                            }
                            compareCount++;
                        }
                        break;
                    case 8://超时
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "超时", Toast.LENGTH_SHORT).show();
                                ReadCompareUtils.getInstance().closeFinger();
                            }
                        });
                        break;
                    default:
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "当前用户id：" + user_ID, Toast.LENGTH_SHORT).show();
                                ReadCompareUtils.getInstance().closeFinger();
                            }
                        });
                        break;
                }
            }
        }, mInputStream);
    }


    /**
     * 监听注册串口数据
     */
    private void checkRegisterData() {
        ReadRegisterUtils.getInstance().startRegisterRead(new FingerListen() {
            @Override
            public void onFail() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, "指纹器初始化失败", Toast.LENGTH_SHORT).show();
                        ReadRegisterUtils.getInstance().closeFinger();
                    }
                });
            }

            @Override
            public void onSuccess(Object readData) {
                int ret = ((ReadData) readData).getRet();
                switch (ret) {
                    case 0://成功
                        switch (count) {
                            case 1://采集第一次返回的结果
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        int ret = FingerManager.getInstance().startFinger_2(userID);
                                        if (ret != 0) {//开始采集指纹信息
                                            ReadRegisterUtils.getInstance().closeFinger();
                                            Toast.makeText(MainActivity.this, "采集指纹失败，请重新采集", Toast.LENGTH_SHORT).show();
                                        } else {
                                            count++;
                                        }
                                    }
                                });
                                break;
                            case 2://采集第二次返回的结果
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        int ret = FingerManager.getInstance().startFinger_3(userID);
                                        if (ret != 0) {//开始采集指纹信息
                                            ReadRegisterUtils.getInstance().closeFinger();
                                            Toast.makeText(MainActivity.this, "采集指纹失败，请重新采集", Toast.LENGTH_SHORT).show();
                                        } else {
                                            count++;
                                        }
                                    }
                                });
                                break;
                            case 3://采集第三次返回的结果
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(MainActivity.this, "采集完成", Toast.LENGTH_SHORT).show();
                                        ReadRegisterUtils.getInstance().closeFinger();
                                    }
                                });
                                break;
                        }
                        break;
                    case 1://失败  重新开始采集
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "采集失败，请重新采集", Toast.LENGTH_SHORT).show();
                                ReadRegisterUtils.getInstance().closeFinger();
                            }
                        });
                        break;
                    case 4://已经满了
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "已经满了", Toast.LENGTH_SHORT).show();
                                ReadRegisterUtils.getInstance().closeFinger();
                            }
                        });
                        break;
                    case 6://用户已存在
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "用户已存在", Toast.LENGTH_SHORT).show();
                                ReadRegisterUtils.getInstance().closeFinger();
                            }
                        });
                        break;
                    case 7://指纹已存在
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "指纹已存在", Toast.LENGTH_SHORT).show();
                                ReadRegisterUtils.getInstance().closeFinger();
                            }
                        });
                        break;
                    case 8://超时
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "超时", Toast.LENGTH_SHORT).show();
                                ReadRegisterUtils.getInstance().closeFinger();
                            }
                        });
                        break;
                }

            }
        }, mInputStream);
    }
}